﻿using Model;
using Business.IFace;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MB.Orm.DB;
using Public.DataAccess;
using MB.RuleBase.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Business
{
    public class NewCommon : IFaceCommon
    {
        public AppUserList LogOn(string userCode, string psw)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                //Microsoft.Practices.EnterpriseLibrary.Data.Database db = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase("NewWeb");
                try
                {
                    AppUserList userInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<AppUserList>("NewWebCommon", "GetUserInfo", userCode).FirstOrDefault();
                    if (userInfo != null)
                    {

                    }
                    else
                    {
                        userInfo = new AppUserList() { ErrorMsg = "登录户名或密码不正确" };
                    }
                    return userInfo;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);

                }

                return null;
            }
        }


        /// <summary>
        /// 更改密码
        /// </summary>
        /// <param name="USERCODE"></param>
        /// <param name="PASSWORD"></param>
        /// <returns></returns>
        public int ChangePwd(string USERCODE, string PASSWORD)
        {
            return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("NewWebCommon", "ChangePwd", USERCODE, PASSWORD);
        }

        public List<AppUserList> GetUserList()
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                List<AppUserList> Userlist = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<AppUserList>("NewWebCommon", "GetUserList", "");
                return Userlist;
            }
        }
        public AppUserList GetUserByCode(string userCode)
        {
            using (var DbScope = new OperationDatabaseScope(new OperationDatabaseContext("NewWeb")))
            {
                try
                {
                    AppUserList userInfo = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<AppUserList>("NewWebCommon", "GetUserInfo", userCode).FirstOrDefault();
                    return userInfo;
                }
                catch (Exception ex)
                {
                    MB.Util.TraceEx.Write(ex.Message);

                }
                return null;
            }
        }
    }
}
